package com.cogent.dynamicform.form.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.cogent.business.helper.EnumHelper;
import com.cogent.common.util.MapUtil;
import com.cogent.common.util.StringUtil;
import com.cogent.dynamicform.cache.EnumCacheManager;
import com.cogent.dynamicform.domain.BaseFormColumnDomain;
import com.cogent.dynamicform.domain.FormColumnDomain;
import com.cogent.dynamicform.form.AbstractDynamicForm;

public class SelectDynamicFormImpl extends AbstractDynamicForm {
	public String genQueryField(BaseFormColumnDomain formColumnDomain, HashMap formMap) {
		StringBuffer str = new StringBuffer();
		String className = "input_middle";
		str.append(EnumHelper.getEnumNoSelect(formColumnDomain.getName(), formColumnDomain.getTypecode()
				, className, MapUtil.getStringValue(formMap, formColumnDomain.getName())));
		
		return str.toString();
	}
	
	public String genInputField(FormColumnDomain formColumnDomain, HashMap formMap) {
		if(formColumnDomain.getViewmodel() > 0)
			return getShowValue(formColumnDomain, formMap) + "&nbsp;";
		StringBuffer str = new StringBuffer();
		String className = "input_middle";
		if(!StringUtil.isEmpty(formColumnDomain.getShowclass()))
			className = formColumnDomain.getShowclass(); 		
		str.append("<select name=\"").append(formColumnDomain.getName()).append("\", id=\"").append(formColumnDomain.getName()).append("\" class=\"").append(className)
		 .append("\"  ");
		if(formColumnDomain.getJs().length()>0)
			str.append(formColumnDomain.getJs());
		str.append(" ").append(formColumnDomain.getShowcontrol()).append(" >");
		String name = formColumnDomain.getName();
		if(name.indexOf(".") > 0)
			name = name.substring(name.indexOf(".") + 1);
		str.append("<option value=\"\">请选择</option>");
		List enmuList = (List)EnumCacheManager.get(formColumnDomain.getTypecode() + "!LIST");
		Iterator it = enmuList.iterator();
		String curValue = MapUtil.getStringValue(formMap, name, formColumnDomain.getDefaultvalue());
		if(StringUtil.isEmpty(curValue))
			curValue = formColumnDomain.getDefaultvalue();
		while(it.hasNext()) {
			HashMap map = (HashMap)it.next();
			String val = MapUtil.getStringValue(map, "value");
			if(StringUtil.isEmpty(val))
				continue;	
			str.append("<option value=\"").append(val).append("\"");
			if(val.equals(curValue))
				str.append(" selected=\"selected\"");
			str.append(">").append(MapUtil.getStringValue(map, "enumitemname")).append("</option>");
		}
		str.append("</select>");
		if(formColumnDomain.isIsrequired())
			str.append("<span class=\"txt_red\">&nbsp;*</span>");
		
		return str.toString();
	}	
	
	public String getShowValue(BaseFormColumnDomain formColumnDomain, HashMap formMap) {
		if(formColumnDomain.getShowtype() == 1)
			return "&nbsp;";
		String name = formColumnDomain.getName();
		if(name.indexOf(".") > 0)
			name = name.substring(name.indexOf(".") + 1);
		String ret = EnumHelper.getEnumValue(formColumnDomain.getTypecode(), MapUtil.getStringValue(formMap, name));
		if(StringUtil.isEmpty(ret))
			ret = "&nbsp;";
		return ret;
	}
}